<?php
/**
 * Formular profilu
 * @author Tomas Srna
 * @package bakalarka
 */

class ProfileForm extends Form 
{
	public function __construct(Template $template, $values = NULL, 
		$action = NULL, $method = 'POST')
	{
		parent::__construct($template, $values, $action, $method);
		
		// name, label, [type], [class], [hint], [$options], [$maxlen]
		$this->items = array(
			new Element('username', L::selected()->username, "text", 
						"required alnum albegin nospace readonly"),
			new Element('email', L::selected()->email, 
						"text", "required email"),
			new Element('firstname', L::selected()->firstname),
			new Element('lastname', L::selected()->lastname),
			new Element('phone', L::selected()->phone, "text", "", 
						L::selected()->usePhoneFormat),
			new Element('address', L::selected()->address, "textarea"),
		);
	}
	
	/**
	 * Pokial nam zakladne kontroly nestacia, mozme si pridat vlastne
	 * specificke pre dany formular
	 */
	public function validate()
	{
		parent::validate();
		
		if($this->getValue('phone') &&
			!array_key_exists('phone', $this->errors))
		{
			if(!preg_match('/^\+\d+$/', $this->getValue('phone')))
				$this->setError('phone', L::selected()->invalidPhone);
		}
		
		if(count($this->errors) > 0)
			return false;
		
		return true;
	}
}
